#include "cube.h"
Cube::Cube():
    step(10.0f)
{
    for( int i = 0; i < 26; i++)
        pBlocks[i] = new Block(vertex_array[i]);
}

Cube::~Cube()
{
    for( int i = 0; i < 26; i++)
        delete pBlocks[i];
}

void Cube::glDraw()
{
    
        
    for( int i = 0; i < 26; i++)
        pBlocks[i]->glDraw();

}


void Cube::rotate(char direction)
{
    //rotate a face acording to the direction
    int blockToRotate[9];
    char rotAix;
    //select blocks to be affected
    switch(direction)   
    {
        case 'f':
            blockToRotate[0] = 17;
            blockToRotate[1] = 20;
            blockToRotate[2] = 23;
            blockToRotate[3] = 14;
            blockToRotate[4] = 6;
            blockToRotate[5] = 3;
            blockToRotate[6] = 0;
            blockToRotate[7] = 9;
            blockToRotate[8] = 12;
            rotAix = 'z';
            break;
        case 'b':
            blockToRotate[0] = 19;
            blockToRotate[1] = 22;
            blockToRotate[2] = 25;
            blockToRotate[3] = 16;
            blockToRotate[4] = 8;
            blockToRotate[5] = 5;
            blockToRotate[6] = 2;
            blockToRotate[7] = 11;
            blockToRotate[8] = 13;
            rotAix = 'z';
            break;
        case 'l':
            blockToRotate[0] = 23;
            blockToRotate[1] = 24;
            blockToRotate[2] = 25;
            blockToRotate[3] = 22;
            blockToRotate[4] = 19;
            blockToRotate[5] = 18;
            blockToRotate[6] = 17;
            blockToRotate[7] = 20;
            blockToRotate[8] = 21;
            rotAix = 'x';
            break;
        case 'r':
            blockToRotate[0] = 6;
            blockToRotate[1] = 7;
            blockToRotate[2] = 8;
            blockToRotate[3] = 5;
            blockToRotate[4] = 2;
            blockToRotate[5] = 1;
            blockToRotate[6] = 0;
            blockToRotate[7] = 3;
            blockToRotate[8] = 4;
            rotAix = 'x';
            std::cout<<"Moving r \n";
            break;
        case 'u':
            blockToRotate[0] = 17;
            blockToRotate[1] = 18;
            blockToRotate[2] = 19;
            blockToRotate[3] = 11;
            blockToRotate[4] = 2;
            blockToRotate[5] = 1;
            blockToRotate[6] = 0;
            blockToRotate[7] = 9;
            blockToRotate[8] = 10;
            rotAix = 'y';
            std::cout<<"Moving u \n";
            break;
        case 'd':
            blockToRotate[0] = 6;
            blockToRotate[1] = 7;
            blockToRotate[2] = 8;
            blockToRotate[3] = 14;
            blockToRotate[4] = 15;
            blockToRotate[5] = 16;
            blockToRotate[6] = 23;
            blockToRotate[7] = 24;
            blockToRotate[8] = 25;
            rotAix = 'y';
            break;
    }
    //rotate in picture
    for( int i = 0 ; i < 9; i++)
        pBlocks[ blockToRotate[i] ]->rotate(rotAix, 3.1415926f/2);
    //swap location in array
    Block* temp[3];
    temp[0] = pBlocks[ blockToRotate[0] ];
    temp[1] = pBlocks[ blockToRotate[1] ];
    temp[2] = pBlocks[ blockToRotate[2] ];

    //0 7 6 -> 2 1 0
    pBlocks[ blockToRotate[0] ] = pBlocks[ blockToRotate[6] ]; 
    pBlocks[ blockToRotate[1] ] = pBlocks[ blockToRotate[7] ]; 
    pBlocks[ blockToRotate[2] ] = pBlocks[ blockToRotate[0] ]; 
    //4 5 6 -> 6 7 0
    pBlocks[ blockToRotate[6] ] = pBlocks[ blockToRotate[4] ]; 
    pBlocks[ blockToRotate[7] ] = pBlocks[ blockToRotate[5] ]; 
    pBlocks[ blockToRotate[0] ] = pBlocks[ blockToRotate[6] ]; 
    //2 3 4 -> 4 5 6
    pBlocks[ blockToRotate[4] ] = pBlocks[ blockToRotate[2] ]; 
    pBlocks[ blockToRotate[5] ] = pBlocks[ blockToRotate[3] ]; 
    pBlocks[ blockToRotate[6] ] = pBlocks[ blockToRotate[4] ]; 
    //0 1 2 -> 2 3 4
    pBlocks[ blockToRotate[2] ] = temp[0];
    pBlocks[ blockToRotate[3] ] = temp[1];
    pBlocks[ blockToRotate[4] ] = temp[2];


}

                

